package com.farm.shop.controller;

import cn.hutool.core.convert.Convert;
import com.farm.common.annotation.Log;
import com.farm.common.utils.PageUtils;
import com.farm.common.utils.Query;
import com.farm.common.utils.R;
import com.farm.common.utils.ShiroUtils;
import com.farm.shop.dao.FarmShopDao;
import com.farm.shop.domain.FarmRoomDO;
import com.farm.shop.domain.FarmShopDO;
import com.farm.shop.response.PlumRoomDetail;
import com.farm.shop.service.FarmRoomService;
import com.farm.shop.service.FarmShopService;
import com.farm.system.domain.UserDO;
import com.farm.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 农家房间表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-09-06 19:44:49
 */

@Controller
@RequestMapping("/farm/farmRoom")
public class FarmRoomController {
    @Autowired
    private FarmRoomService farmRoomService;

    @Autowired
    private FarmShopService farmShopService;

    @Autowired
    private FarmShopDao farmShopDao;

    /**
     * 住宿农家搜索
     *
     * @param params 参数
     * @return List<FarmRoomDO>
     */
    @ResponseBody
    @GetMapping(value = "/app/room/search")
    public Result getAppRoomSearch(@RequestParam Map<String, Object> params) {
        Object keyword = params.get("roomKeyword");
        String dishKeyword = Convert.toStr(keyword);
        List<FarmRoomDO> search = farmRoomService.search(dishKeyword);
        if (search.size() > 0) {
            return Result.success(search);
        } else {
            return Result.success("抱歉，没有符合条件的美食");
        }

    }

    /**
     * 用户版   查询房间详情
     *
     * @return List<FarmRoomDO>
     */
    @ResponseBody
    @GetMapping(value = "/app/roomdetail")
    public Result getAppRoomDetailByid(@RequestParam Map<String, Object> params) {
        List<FarmRoomDO> roomdetail = farmRoomService.list(params);
        PlumRoomDetail result = new PlumRoomDetail();
        result.setWrapper(roomdetail);
        FarmShopDO farmShop = farmShopService.get(roomdetail.get(0).getShopId());
        result.setPhoneNumber(farmShop.getPhone());
        return Result.success(result);
    }

    /**
     * 根据商家 id 查询该商家的房间列表
     */
    @ResponseBody
    @GetMapping(value = "/app/rooms")
    public Result getAppRoomsByShopId(@RequestParam Map<String, Object> params) {

        // 房间上下架    1上架  2下架
        params.put("updownStatus", 1);

        //查询列表数据
        Query query = new Query(params);

        List<FarmRoomDO> appRoomsByShopId = farmRoomService.list(query);
        return Result.success(appRoomsByShopId);
    }

    @GetMapping()
    @RequiresPermissions("farm:farmRoom:farmRoom")
    String FarmRoom() {
        UserDO user = ShiroUtils.getUser();
        // 16 代表平台管理员
        return  user.getDeptId() == 16 ? "farm/farmRoom/platformFarmRoom"
               : "farm/farmRoom/farmRoom";


    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("farm:farmRoom:farmRoom")
    public PageUtils list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        UserDO user = ShiroUtils.getUser();
        Long userId = ShiroUtils.getUserId();
        Long shopId = null;

        // 判断角色
        if (user.getDeptId() != 16){
            params.put("userId", userId);
            Query query = new Query(params);
            List<FarmShopDO> farmShop = farmShopService.list(query);
            shopId = farmShop.get(0).getId();
        }

        if (shopId != null){
            params.put("shopId", shopId);
        }

        Query query = new Query(params);
        List<FarmRoomDO> farmRoomList = farmRoomService.list(query);
        int total = farmRoomService.count(query);
        PageUtils pageUtils = new PageUtils(farmRoomList, total);
        return pageUtils;
    }

    @Log(value = "B模糊查询房间列表")
    @ResponseBody
    @GetMapping("/searchName")
    @RequiresPermissions("farm:farmRoom:farmRoom")
    public PageUtils searchName(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<FarmRoomDO> farmRoomList = farmRoomService.searchName(query);
        int total = farmRoomService.searchNameCount(query);
        PageUtils pageUtils = new PageUtils(farmRoomList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("farm:farmRoom:add")
    String add() {
        return "farm/farmRoom/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("farm:farmRoom:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        FarmRoomDO farmRoom = farmRoomService.get(id);
        String[] scenicpictureArr = farmRoom.getRoomPhotos().split(",");
        if (scenicpictureArr.length==1&&"".equals(scenicpictureArr[0])){
            scenicpictureArr = null;
        }
        model.addAttribute("farmRoom", farmRoom);
        model.addAttribute("scenicpictureArr", scenicpictureArr);
        return "farm/farmRoom/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("farm:farmRoom:add")
    public R save(FarmRoomDO farmRoom) {
        // 根据 userId 查询店铺 id
        Long userId = ShiroUtils.getUserId();
        Long shopId = farmShopDao.getShopId(userId);
        farmRoom.setShopId(shopId);
        if (farmRoomService.save(farmRoom) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("farm:farmRoom:edit")
    public R update(FarmRoomDO farmRoom) {
        farmRoomService.update(farmRoom);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("farm:farmRoom:remove")
    public R remove(Long id) {
        if (farmRoomService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("farm:farmRoom:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        farmRoomService.batchRemove(ids);
        return R.ok();
    }

    @GetMapping("/roomPreview/{id}")
    String roomPreview(@PathVariable("id") Long id, Model model) {
//        model.addAttribute("termId", termId);
        FarmRoomDO farmRoom = farmRoomService.get(id);
        String[] scenicpictureArr = farmRoom.getRoomPhotos().split(",");
        model.addAttribute("farmRoom", farmRoom);
        model.addAttribute("scenicpictureArr", scenicpictureArr);
        return "farm/farmRoom/roomBanner";
    }

}
